/**
 * 
 */
package info.niwota.ezb;

import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.content.Context;
import android.util.Log;

/**
 * @author qiangli
 * 
 */
public final class KeyguardUtil {
	private static final String TAG = "KeyguardUtil";

	private static KeyguardLock keyguard;

	private static KeyguardManager km;

	synchronized public static void disableGuard(Context ctx) {
		if (Debug.DEBUG) {
			Log.d(TAG, "disableGuard: " + keyguard);
		}
		if (km == null) {
			km = (KeyguardManager) ctx
					.getSystemService(Context.KEYGUARD_SERVICE);

			if (Debug.DEBUG) {
				Log.d(TAG, "km: " + km);
			}
		}

		if (km.inKeyguardRestrictedInputMode()) {
			if (keyguard != null) {
				keyguard.disableKeyguard();
			} else {
				keyguard = km.newKeyguardLock(TAG);
				keyguard.disableKeyguard();
			}

			if (Debug.DEBUG) {
				Log.d(TAG, "km: " + km);
			}
		} else {
			keyguard = null;
		}
	}

	synchronized public static void enableGuard() {
		if (Debug.DEBUG) {
			Log.d(TAG, "enableGuard: " + keyguard);
		}

		if (km != null) {
			if (keyguard != null) {
				keyguard.reenableKeyguard();
			}
			keyguard = null;
		}
	}

	synchronized public static boolean isKeyguarded(Context ctx) {
		boolean mode = false;

		if (km != null) {
			mode = km.inKeyguardRestrictedInputMode();
		}

		if (Debug.DEBUG) {
			Log.d(TAG, "isKeyguarded: " + keyguard + " mode: " + mode);
		}

		return mode;
	}

	/**
	 * 
	 */
	private KeyguardUtil() {
	}
}
